package com.dk.controller;

import com.dk.pojo.TabCategory;
import com.dk.pojo.TabRoute;
import com.dk.pojo.TabUser;
import com.dk.service.CategoryService;
import com.dk.service.CollectService;
import com.dk.service.RouteService;
import com.github.pagehelper.PageInfo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpSession;
import java.util.List;


@Controller
@RequestMapping("indexController")
public class IndexController {
    @Autowired
    RouteService routeService;
    @Autowired
    CollectService collectService;
    @Autowired
    CategoryService categoryService;


    @RequestMapping("findUser")
    @ResponseBody
    public boolean findUser(@RequestParam("rid")Integer rid, HttpServletRequest request){
        request.setAttribute("rid",rid);
        return true;
    }
    @RequestMapping("isUserCollect")
    @ResponseBody
    public boolean isUserCollect(@RequestParam("info[]")Integer[] info){
        Integer uid = info[0];
        if (uid!=null){
            Integer rid = info[1];
            //TabRoute route = routeService.getInfoByRid(rid);
            List<Integer> rids = collectService.selectCollectByUid(uid);
            return rids.contains(rid);
        }
        return false;
    }
    @RequestMapping("toRouteListSearch")
    public String toRouteList(TabRoute tabRoute,
                              @RequestParam(value = "pageNum", required = false, defaultValue = "1") Integer pageNum,
                              @RequestParam(value = "pageSize", required = false, defaultValue = "5") Integer pageSize,
                              Model model) {
        //System.out.println(tabRoute.getRname());
        //获取路线类型
        List<TabCategory> categories = categoryService.getInfo();
        //获取热门推荐数据 根据收藏量排序
        List<TabRoute> tabRoutesPopular = collectService.getInfoLimitFour();
        //根据路线类型返回的路线集合 并做分页 模糊查询
        PageInfo<TabRoute> pageInfo = routeService.getInfoRouteForFuzzyQuery(pageNum, pageSize, tabRoute);
        model.addAttribute("categories", categories);
        model.addAttribute("pageInfo", pageInfo);
        model.addAttribute("tabRoutesPopular",tabRoutesPopular);
        model.addAttribute("route",tabRoute);
        return "route_list_search";
    }

    /*
      用户退出方法
     */
    @RequestMapping("loginOut")
    @ResponseBody
    public boolean loginOut(HttpServletRequest request){
        HttpSession session = request.getSession();
        session.removeAttribute("userLogin");
        return true;
    }


}
